package com.xy.shop.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.xy.shop.common.dtgrid.model.Column;
import com.xy.shop.common.dtgrid.model.Pager;
import jxl.SheetSettings;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.write.*;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;

public class ExportUtils {
    public static Logger logger = LoggerFactory.getLogger(ExportUtils.class);
    /**
     * 导出
     *
     * @param:request
     *            请求对象
     * @param response
     *            响应对象
     * @param pager
     *            GridPager对象
     * @param:exportDatas
     *            导出的数据
     * @param:fileName
     *            文件名
     * @throws Exception
     */
    public static void export(HttpServletResponse response, Pager pager)
            throws Exception {
        // 获取文件名
        logger.info("export:"+pager.getExportDatas());
        String fileName = "datas";
        fileName = URLEncoder.encode(pager.getExportFileName(), "UTF-8");
        // 导出excel
        if (ExportType.EXCEL.name().equalsIgnoreCase(pager.getExportType())) {
            if(pager!=null &&  pager.getExportDatas()!=null && pager.getExportDatas().size()>65500){
                ExportUtils.writeExcel(response,pager, pager.getExportDatas(),
                        pager.getExportFileName());
            }else{
                ExportUtils.exportExcel(response, pager, pager.getExportDatas(),
                        fileName);
            }
            return;
        }
        // 导出cvs
        if (ExportType.CSV.name().equalsIgnoreCase(pager.getExportType())) {
            ExportUtils.exportCsv(response, pager, pager.getExportDatas(),
                    fileName);
            return;
        }
        // 导出txt
        if (ExportType.TXT.name().equalsIgnoreCase(pager.getExportType())) {
            ExportUtils.exportTxt(response, pager, pager.getExportDatas(),
                    fileName);
            return;
        }
        // 导出pdf
        if (ExportType.PDF.name().equalsIgnoreCase(pager.getExportType())) {
            ExportUtils.exportPdf(response, pager, pager.getExportDatas(),
                    fileName);
            return;
        }
    }

    /**
     * 导出
     *
     * @param:request
     *            请求对象
     * @param response
     *            响应对象
     * @param pager
     *            GridPager对象
     * @param:exportDatas
     *            导出的数据
     * @param:fileName
     *            文件名
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public static void exportAll(HttpServletResponse response, Pager pager, List<?> list)
            throws Exception {
        List<Map<String, Object>> datas = new ArrayList<Map<String,Object>>();
        String json = JSON.toJSONString(list, SerializerFeature.WriteDateUseDateFormat);
        datas = JSON.parseObject(json, datas.getClass());
        pager.setExportDatas(datas);
        export(response,pager);
    }


    /**
     * 导出Excel
     *
     * @param:request
     *            请求对象
     * @param response
     *            响应对象
     * @param pager
     *            GridPager对象
     * @param exportDatas
     *            导出的数据
     * @param fileName
     *            文件名
     * @throws Exception
     */
    private static void exportExcel(HttpServletResponse response, Pager pager,
									List<Map<String, Object>> exportDatas, String fileName)
            throws Exception {
        // 设置响应头
        response.setContentType("application/vnd.ms-excel");
        // 执行文件写入
        response.setHeader("Content-Disposition", "attachment;filename="
                + fileName + ".xls");
        // 获取输出流
        OutputStream outputStream = response.getOutputStream();
        // 定义Excel对象
        WritableWorkbook book = jxl.Workbook.createWorkbook(outputStream);
        // 创建Sheet页
        WritableSheet sheet = book.createSheet("数据", 0);
        // 冻结表头
        SheetSettings settings = sheet.getSettings();
        settings.setVerticalFreeze(1);
        // 定义表头字体样式、表格字体样式
        WritableFont headerFont = new WritableFont(
                WritableFont.createFont("Lucida Grande"), 9, WritableFont.BOLD);
        WritableFont bodyFont = new WritableFont(
                WritableFont.createFont("Lucida Grande"), 9,
                WritableFont.NO_BOLD);
        WritableCellFormat headerCellFormat = new WritableCellFormat(headerFont);
        WritableCellFormat bodyCellFormat = new WritableCellFormat(bodyFont);
        // 设置表头样式：加边框、背景颜色为淡灰、居中样式
        headerCellFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
        headerCellFormat.setBackground(Colour.PALE_BLUE);
        headerCellFormat.setAlignment(Alignment.CENTRE);
        headerCellFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
        // 设置表格体样式：加边框、居中
        bodyCellFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
        bodyCellFormat.setAlignment(Alignment.CENTRE);
        bodyCellFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
        // 判断一下表头数组是否有数据
        if (pager.getExportColumns() != null
                && pager.getExportColumns().size() > 0) {
            // 循环写入表头
            int seq = 0;
            for (Column column : pager.getExportColumns()) {
                sheet.addCell(new Label(seq, 0, column.getTitle(),
                        headerCellFormat));
                seq++;
            }
            // 判断表中是否有数据
            if (exportDatas != null && exportDatas.size() > 0) {
                // 循环写入表中数据
                for (int i = 0; i < exportDatas.size(); i++) {
                    Map<String, Object> record = (Map<String, Object>) exportDatas
                            .get(i);
                    seq = 0;
                    for (int j = 0; j < pager.getExportColumns().size(); j++) {
                        Column column = pager.getExportColumns().get(j);
                        String content = MapUtils.getString(record,
                                column.getId());
                        // 如果内容未被处理则进行格式化
                        if (!pager.getExportDataIsProcessed()) {
                            content = GridUtils.formatContent(column, content);
                        }
                        if(StringUtils.isNotBlank(content) && content.matches("^(-?\\d+)(\\.\\d+)?$") &&content.length()<11){
                            sheet.addCell(new jxl.write.Number(seq, i + 1, Double.parseDouble(content),bodyCellFormat));
                        }
                        else {
                            sheet.addCell(new Label(seq, i + 1, content,
                                    bodyCellFormat));
                        }
                        seq++;
                    }
                }
            }
            // 写入Excel工作表
            book.write();
            // 关闭Excel工作薄对象
            book.close();
            // 关闭流
            outputStream.flush();
            outputStream.close();
            outputStream = null;
        }
    }

    /**
     * 导出Csv
     *
     * @param:request
     *            请求对象
     * @param response
     *            响应对象
     * @param pager
     *            GridPager对象
     * @param exportDatas
     *            导出的数据
     * @param fileName
     *            文件名
     * @throws Exception
     */
    private static void exportCsv(HttpServletResponse response, Pager pager,
								  List<Map<String, Object>> exportDatas, String fileName)
            throws Exception {
        // 设置响应头
        response.setContentType("application/CSV");
        // 执行文件写入
        response.setHeader("Content-Disposition", "attachment;filename="
                + fileName + ".csv");
        // 定义文件内容
        StringBuffer buffer = new StringBuffer();
        // 判断一下表头数组是否有数据
        if (pager.getExportColumns() != null
                && pager.getExportColumns().size() > 0) {
            // 循环写入表头
            for (int i = 0; i < pager.getExportColumns().size(); i++) {
                Column column = pager.getExportColumns().get(i);
                buffer.append(column.getTitle()).append(
                        i == (pager.getExportColumns().size() - 1) ? "" : ",");
            }
            // 换行
            buffer.append("\n");
            // 判断表中是否有数据
            if (exportDatas != null && exportDatas.size() > 0) {
                // 循环写入表中数据
                for (int i = 0; i < exportDatas.size(); i++) {
                    Map<String, Object> record = (Map<String, Object>) exportDatas
                            .get(i);
                    for (int j = 0; j < pager.getExportColumns().size(); j++) {
                        Column column = pager.getExportColumns().get(j);
                        String content = MapUtils.getString(record,
                                column.getId());
                        // 如果内容未被处理则进行格式化
                        if (!pager.getExportDataIsProcessed()) {
                            content = GridUtils.formatContent(column, content);
                        }
                        buffer.append("\"")
                                .append(content)
                                .append("\"")
                                .append(j == (pager.getExportColumns().size() - 1) ? ""
                                        : ",");
                    }
                    // 换行
                    buffer.append("\n");
                }
            }
        }
        // 以UTF-8字符集写入
        OutputStreamWriter writer = new OutputStreamWriter(
                response.getOutputStream(), "UTF-8");
        writer.write(new String(new byte[] { (byte) 0xEF, (byte) 0xBB,
                (byte) 0xBF }));
        writer.write(buffer.toString());
        writer.flush();
        writer.close();
    }

    /**
     * 导出Txt
     *
     * @param:request
     *            请求对象
     * @param response
     *            响应对象
     * @param pager
     *            GridPager对象
     * @param exportDatas
     *            导出的数据
     * @param fileName
     *            文件名
     * @throws Exception
     */
    private static void exportTxt(HttpServletResponse response, Pager pager,
								  List<Map<String, Object>> exportDatas, String fileName)
            throws Exception {
        // 设置响应头
        response.setContentType("application/txt");
        // 执行文件写入
        response.setHeader("Content-Disposition", "attachment;filename="
                + fileName + ".txt");
        // 定义文件内容
        StringBuffer buffer = new StringBuffer();
        // 判断一下表头数组是否有数据
        if (pager.getExportColumns() != null
                && pager.getExportColumns().size() > 0) {
            // 循环写入表头
            for (int i = 0; i < pager.getExportColumns().size(); i++) {
                Column column = pager.getExportColumns().get(i);
                buffer.append(column.getTitle()).append(
                        i == (pager.getExportColumns().size() - 1) ? "" : "\t");
            }
            // 换行
            buffer.append("\r\n");
            // 判断表中是否有数据
            if (exportDatas != null && exportDatas.size() > 0) {
                // 循环写入表中数据
                for (int i = 0; i < exportDatas.size(); i++) {
                    Map<String, Object> record = (Map<String, Object>) exportDatas
                            .get(i);
                    for (int j = 0; j < pager.getExportColumns().size(); j++) {
                        Column column = pager.getExportColumns().get(j);
                        String content = MapUtils.getString(record,
                                column.getId());
                        // 如果内容未被处理则进行格式化
                        if (!pager.getExportDataIsProcessed()) {
                            content = GridUtils.formatContent(column, content);
                        }
                        buffer.append(content).append(
                                j == (pager.getExportColumns().size() - 1) ? ""
                                        : "\t");
                    }
                    // 换行
                    buffer.append("\r\n");
                }
            }
        }
        // 以UTF-8字符集写入
        OutputStreamWriter writer = new OutputStreamWriter(
                response.getOutputStream(), "UTF-8");
        writer.write(buffer.toString());
        writer.flush();
        writer.close();
    }

    /**
     * 导出Pdf
     *
     * @param:request
     *            请求对象
     * @param response
     *            响应对象
     * @param pager
     *            GridPager对象
     * @param exportDatas
     *            导出的数据
     * @param fileName
     *            文件名
     * @throws Exception
     */
    private static void exportPdf(HttpServletResponse response, Pager pager,
								  List<Map<String, Object>> exportDatas, String fileName)
            throws Exception {
        // 设置响应头
        response.setContentType("application/pdf");
        // 执行文件写入
        response.setHeader("Content-Disposition", "attachment;filename="
                + fileName + ".pdf");
        // 获取输出流
        OutputStream outputStream = response.getOutputStream();
        // 创建字体对象(STSong-Light，AdobeSongStd-Light-Acro，及STSongStd-Light-Acro是Acrobat的三种简体中文标准字体)
        BaseFont chinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
                BaseFont.NOT_EMBEDDED);
        com.itextpdf.text.Font headerFont = new com.itextpdf.text.Font(chinese, 9, com.itextpdf.text.Font.BOLD);
        com.itextpdf.text.Font bodyFont = new com.itextpdf.text.Font(chinese, 9, com.itextpdf.text.Font.NORMAL);
        // 设置PDF页面大小
        Rectangle rect = new Rectangle(PageSize.A4.rotate());
        // 创建写入对象
        Document document = new Document(rect);
        PdfWriter writer = PdfWriter.getInstance(document, outputStream);
        // 设置PDF版本(默认1.4)
        writer.setPdfVersion(PdfWriter.PDF_VERSION_1_2);
        // 设置文档属性
        document.addTitle("");
        document.addAuthor("");
        document.addSubject("");
        document.addKeywords("");
        document.addCreator("");
        // 页边空白
        document.setMargins(0, 0, 40, 40);
        // 打开文档对象
        document.open();
        // 计算表格列数
        int columnCount = pager.getExportColumns().size();
        // 创建表格
        PdfPTable table = new PdfPTable(columnCount);
        // 定义单元格用于写入数据
        PdfPCell cell;
        // 判断一下表头数组是否有数据
        if (pager.getExportColumns() != null
                && pager.getExportColumns().size() > 0) {
            // 循环写入表头
            for (int i = 0; i < pager.getExportColumns().size(); i++) {
                Column column = pager.getExportColumns().get(i);
                // 赋值并设置背景色及居中
                cell = new PdfPCell(new Phrase(column.getTitle(), headerFont));
                cell.setBackgroundColor(BaseColor.CYAN);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setVerticalAlignment(Element.ALIGN_CENTER);
                table.addCell(cell);
            }
            // 判断表中是否有数据
            if (exportDatas != null && exportDatas.size() > 0) {
                // 循环写入表中数据
                for (int i = 0; i < exportDatas.size(); i++) {
                    Map<String, Object> record = (Map<String, Object>) exportDatas
                            .get(i);
                    for (int j = 0; j < pager.getExportColumns().size(); j++) {
                        Column column = pager.getExportColumns().get(j);
                        String content = MapUtils.getString(record,
                                column.getId());
                        // 如果内容未被处理则进行格式化
                        if (!pager.getExportDataIsProcessed()) {
                            content = GridUtils.formatContent(column, content);
                        }
                        // 赋值并居中
                        cell = new PdfPCell(new Phrase(content, bodyFont));
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        cell.setVerticalAlignment(Element.ALIGN_CENTER);
                        table.addCell(cell);
                    }
                }
            }
            // 写入PDF工作表
            document.add(table);
            // 关闭PDF文档工作薄对象
            document.close();
            // 关闭流
            outputStream.flush();
            outputStream.close();
            outputStream = null;
        }
    }










    /**
     * 日期转化为字符串,格式为yyyy-MM-dd HH:mm:ss
     */
    private static String getCnDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    /**
     * Excel 导出，POI实现，先写入Excel标题，与writeExcelData配合使用
     * 先使用writeExcelTitle再使用writeExcelData
     *
     */
    public static void  writeExcel(HttpServletResponse response, Pager pager, List<Map<String, Object>> exportDatas, String fileName) throws IOException {
        // 设置响应头
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        // 执行文件写入
        response.setHeader("Content-Disposition", "attachment;filename="
                + URLEncoder.encode(fileName+".xlsx", "utf-8"));

        //客户端不缓存
        response.addHeader("Pargam", "no-cache");
        response.addHeader("Cache-Control", "no-cache");


        // 获取输出流
        OutputStream outputStream = response.getOutputStream();

        if (pager.getExportColumns() != null && pager.getExportColumns().size() > 0) {

            exportExcelTitle(outputStream, pager,exportDatas);

        }


    }

    /**
     * 导出字符串数据
     */

    private static void exportExcelTitle( OutputStream outputStream , Pager pager, List<Map<String, Object>> exportDatas) throws IOException {

        //Excel当前数据行数(将要写入数据的索引数)
        int currentRowNum = 0;
        //默认列宽度
        int DEFAULT_COLUMN_SIZE = 11;
        //刷新写入硬盘数据阀值
        int flushRows = 1000;

        XSSFWorkbook tplWorkBook = new XSSFWorkbook();
        Workbook writeDataWorkBook = new SXSSFWorkbook(tplWorkBook, flushRows);
        Map<String, CellStyle> cellStyleMap = styleMap(writeDataWorkBook);


        // 表头样式
        CellStyle headStyle = cellStyleMap.get("head");
        // 生成一个表格
        Sheet sheet = writeDataWorkBook.createSheet("数据");
        // 正文样式
        CellStyle contentStyle = cellStyleMap.get("content");
        //正文整数样式
        CellStyle contentIntegerStyle = cellStyleMap.get("integer");
        //正文带小数整数样式
        CellStyle contentDoubleStyle = cellStyleMap.get("double");
        // 设置表格默认列宽度
        sheet.setDefaultColumnWidth(DEFAULT_COLUMN_SIZE);

        //写入标题
        Row row = sheet.createRow(currentRowNum);
        for (int i = 0; i < pager.getExportColumns().size(); i++) {
            Cell cell = row.createCell(i);
            cell.setCellStyle(headStyle);
            RichTextString text = new XSSFRichTextString( pager.getExportColumns().get(i).getTitle());
            cell.setCellValue(text);
        }
        //写入成功一行数据递增行数
        currentRowNum = currentRowNum + 1;

        //写入数据
        if (exportDatas != null && exportDatas.size() > 0) {
            for (Map<String, Object> record : exportDatas) {
                row = sheet.createRow(currentRowNum);

                for (int j = 0; j < pager.getExportColumns().size(); j++) {
                    Column column = pager.getExportColumns().get(j);
                    Object dataObject = MapUtils.getString(record,
                            column.getId());
                    // 如果内容未被处理则进行格式化
                    if (!pager.getExportDataIsProcessed()) {
                        dataObject = GridUtils.formatContent(column, (String)dataObject);
                    }
                    Cell contentCell = row.createCell(j);

                        //设置单元格显示格式
                    if (dataObject != null) {
                        if ( NumberUtils.isDigits(dataObject.toString()) && dataObject.toString().length()<11) {
                            contentCell.setCellStyle(contentIntegerStyle);
                            contentCell.setCellValue(Integer.parseInt(dataObject.toString()));
                        } else if (!NumberUtils.isDigits(dataObject.toString()) && NumberUtils.isNumber(dataObject.toString()) ) {
                            contentCell.setCellStyle(contentDoubleStyle);
                            contentCell.setCellValue(Double.parseDouble(dataObject.toString()));
                        }  else {
                            contentCell.setCellStyle(contentStyle);
                            contentCell.setCellValue(dataObject.toString());
                        }
                    } else {
                        contentCell.setCellStyle(contentStyle);
                        // 设置单元格内容为字符型
                        if(j==0){
                            contentCell.setCellValue(currentRowNum);
                        }else{
                            contentCell.setCellValue(0);
                        }

                    }
                }
                //写入成功一行数据递增行数
                currentRowNum = currentRowNum + 1;
            }
        }


        try {
            ((SXSSFSheet) sheet).flushRows(flushRows);
            dispose(writeDataWorkBook,outputStream,cellStyleMap);

        } catch (IOException e) {
            throw new IOException(e);
        }
    }

    /**
     * 创建单元格表头样式
     *
     * @param workbook 工作薄
     */

    private static CellStyle createCellHeadStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        // 设置边框样式
        style.setBorderBottom(XSSFCellStyle.BORDER_THIN);
        style.setBorderLeft(XSSFCellStyle.BORDER_THIN);
        style.setBorderRight(XSSFCellStyle.BORDER_THIN);
        style.setBorderTop(XSSFCellStyle.BORDER_THIN);
        //设置对齐样式
        style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
        // 生成字体
        Font font = workbook.createFont();
        // 表头样式
        style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
        style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
        font.setFontHeightInPoints((short) 9);
        font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
        // 把字体应用到当前的样式
        style.setFont(font);
        return style;
    }


    /**
     * 创建单元格正文样式
     *
     * @param workbook 工作薄
     */
    private static CellStyle createCellContentStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        // 设置边框样式
        style.setBorderBottom(XSSFCellStyle.BORDER_THIN);
        style.setBorderLeft(XSSFCellStyle.BORDER_THIN);
        style.setBorderRight(XSSFCellStyle.BORDER_THIN);
        style.setBorderTop(XSSFCellStyle.BORDER_THIN);
        //设置对齐样式
        style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
        // 生成字体
        Font font = workbook.createFont();
        // 正文样式
        style.setFillPattern(XSSFCellStyle.NO_FILL);
        style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
        font.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL);
        // 把字体应用到当前的样式
        style.setFont(font);
        return style;
    }

    /**
     * 单元格样式(Integer)列表
     */

    private static CellStyle createCellContent4IntegerStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        // 设置边框样式
        style.setBorderBottom(XSSFCellStyle.BORDER_THIN);
        style.setBorderLeft(XSSFCellStyle.BORDER_THIN);
        style.setBorderRight(XSSFCellStyle.BORDER_THIN);
        style.setBorderTop(XSSFCellStyle.BORDER_THIN);
        //设置对齐样式
        style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
        // 生成字体
        Font font = workbook.createFont();
        // 正文样式
        style.setFillPattern(XSSFCellStyle.NO_FILL);
        style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
        font.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL);
        // 把字体应用到当前的样式
        style.setFont(font);
        style.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0"));
        //数据格式只显示整数
        return style;
    }

    /**
     * 单元格样式(Double)列表
     */
    private static CellStyle createCellContent4DoubleStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        // 设置边框样式
        style.setBorderBottom(XSSFCellStyle.BORDER_THIN);
        style.setBorderLeft(XSSFCellStyle.BORDER_THIN);
        style.setBorderRight(XSSFCellStyle.BORDER_THIN);
        style.setBorderTop(XSSFCellStyle.BORDER_THIN);
        //设置对齐样式
        style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
        // 生成字体
        Font font = workbook.createFont();
        // 正文样式
        style.setFillPattern(XSSFCellStyle.NO_FILL);
        style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
        font.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL);
        // 把字体应用到当前的样式
        style.setFont(font);
        style.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0.00"));
        //保留两位小数点
        return style;
    }

    /**
     * 单元格样式列表
     */
    private static Map<String, CellStyle> styleMap(Workbook workbook) {
        Map<String, CellStyle> styleMap = new LinkedHashMap<>();
        styleMap.put("head", createCellHeadStyle(workbook));
        styleMap.put("content", createCellContentStyle(workbook));
        styleMap.put("integer", createCellContent4IntegerStyle(workbook));
        styleMap.put("double", createCellContent4DoubleStyle(workbook));
        return styleMap;
    }


    /**
     * 释放资源
     */
    public static void dispose(Workbook writeDataWorkBook, OutputStream outputStream, Map cellStyleMap) throws IOException {
        try {
            if (writeDataWorkBook != null) {
                writeDataWorkBook.write(outputStream);
            }
            if (outputStream != null) {
                outputStream.flush();
                outputStream.close();
            }
            if (cellStyleMap != null) {
                cellStyleMap.clear();
            }
            cellStyleMap = null;
            outputStream = null;
            writeDataWorkBook = null;
        } catch (Exception e) {
            throw new IOException(e);
        }
    }







}
